package com.xuanxu.blogs.framework.mybatisplus.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.xuanxu.blogs.framework.mybatisplus.core.handler.DataAutoFillMetaObjectHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @author XuanXu
 * @version 1.0
 * @date 2024-08-18 21:58
 */
@AutoConfiguration
@EnableConfigurationProperties(MybatisPlusProperties.class)
@MapperScan("${xuanxu.mybatis-plus.mapper-scan-package}")
public class MybatisPlusConfiguration {

    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 如果配置多个插件, 切记分页最后添加
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType
        return interceptor;
    }

    /**
     * 数据自动填充元数据处理器
     */
    @Bean
    public DataAutoFillMetaObjectHandler dataAutoFillMetaObjectHandler() {
        return new DataAutoFillMetaObjectHandler();
    }
}
